/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Chitietphieunhaphang;
import pojo.Chuongtrinhkhuyenmai;
import pojo.Hinhthuckhuyenmai1;
import pojo.Hinhthuckhuyenmai2;
import pojo.Mathang;
import pojo.Nhacungcap;
import pojo.Nhavien;
import pojo.Phieunhaphang;
import pojo.Sanphamtangkem;
import util.HibernateUtil;

/**
 *
 * @author FT
 */
public class MatHangDAO {
     public static List<Mathang> layDanhSachMatHang() {
        List<Mathang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select sv from Mathang sv";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
                //Log the exception
                System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }
     
     public static Mathang GetMatHang(String ids){
         Mathang mh = new Mathang();
         List<Mathang> listmh = MatHangDAO.layDanhSachMatHang();
         for (int i=0;i<listmh.size();i++){
             if (ids.equals(listmh.get(i).getIdmathang())){
                 mh = listmh.get(i);
             }
         }
         return mh;
     }
    
     public static boolean capNhatMatHang(Mathang matHang){
       Session session = HibernateUtil.getSessionFactory().openSession();
       boolean kq = true;
       Transaction transaction = null;
       try {
          transaction = session.beginTransaction();
          session.saveOrUpdate(matHang); 
          transaction.commit();
       } catch (Exception e) {
           transaction.rollback(); 
           System.err.println(e); 
           kq = false;
       } finally { session.close(); }
       return kq;
   }
     public static int KiemTraLuongTon(Mathang mh, int sl){
         //0:khong thoa dieu kien toi thieu, 1 thoa, 2 khong thoa dieu kien toi da
         int co = 1;
         int tong = mh.getSoluongtonkhotoithieu() + sl;
         if (mh.getSoluongtonkhohientai() < mh.getSoluongtonkhotoithieu()){
             return 0;
         }
         if (tong > mh.getSoluongtonkhotoida()){
             return 2;
         }
         return co;
     }
     
     public static boolean ThemMatHang(Mathang mh, int sl){
         int luongton = sl + mh.getSoluongtonkhohientai();
            Session session = HibernateUtil.getSessionFactory().openSession();

        try {
            session.beginTransaction();
            mh.setSoluongtonkhohientai(luongton);
            session.update(mh);
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
            return false;
        } finally {
            session.close();
        }
        return true;
     }
     
     public static String LayNhaSanXuat(String idx){
         String nsx="";
         Mathang mh = MatHangDAO.GetMatHang(idx);
         //nsx = mh.getNhacungcap().getTennhacungcap();
         
         return nsx;
     }
     
     public static void ThemNhaCungCap(String mathang, String nhacungcap){
          Mathang mh = MatHangDAO.GetMatHang(mathang);
          Nhacungcap ncc = NhaCungCapDAO.getNhaCungCap(nhacungcap);
           Session session = HibernateUtil.getSessionFactory().openSession();
            try {
            session.beginTransaction();
            mh.setNhacungcap(ncc);
            session.update(mh);
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
            
        } finally {
            session.close();
        }
     }
     public static int CheckHinhThucKhuyenMai(String masp, int soluong, Date date){
         List<Chuongtrinhkhuyenmai> listkm = new ArrayList<Chuongtrinhkhuyenmai>();
         int flag = 0;
         listkm = ChiTietKhuyenMaiDAO.layDanhSachChuongTrinhKhuyenMai();
         for (int i=0;i<listkm.size();i++){
             if (masp.equals(listkm.get(i).getMathang().getIdmathang())){
                 Date ngaybatdau = listkm.get(i).getNgaybatdau();
                 Date ngayketthuc = listkm.get(i).getNgayketthuc();
                 if ((date.compareTo(ngaybatdau)>0) && (date.compareTo(ngayketthuc)<0)){
                     int hinhthuc = listkm.get(i).getHinhthuc();
                     if (hinhthuc == 1){
                         if (CheckSoLuongHinhThuc1(listkm.get(i).getIdchuongtrinhkhuyenmai(), soluong)==1)
                             return 1;
                     }
                     if (hinhthuc==2){
                         if (CheckSoLuongHinhThuc2(listkm.get(i).getIdchuongtrinhkhuyenmai(), soluong)==1)
                             return 2;
                     }
                 }
             }
         }
         return flag;
     }
     
     public static int CheckSoLuongHinhThuc1(String mactkm, int soluong){
         List<Hinhthuckhuyenmai1> listht = new ArrayList<Hinhthuckhuyenmai1>();
         listht = HinhThucKhuyenMai1DAO.layDanhSachHinhThucKhuyenMai1();
         for (int i=0;i<listht.size();i++){
             if (mactkm.equals(listht.get(i).getChuongtrinhkhuyenmai().getIdchuongtrinhkhuyenmai())){
                 if(listht.get(i).getSoluongmua() <= soluong)
                     return 1;
             }
         }
         return 0;
     }
     
     public static int CheckSoLuongHinhThuc2(String mactkm, int soluong){
         List<Hinhthuckhuyenmai2> listkm = HinhThucKhuyenMai2DAO.layDanhSachHinhThucKhuyenMai2();
         for (int i=0;i<listkm.size();i++){
             if (mactkm.equals(listkm.get(i).getChuongtrinhkhuyenmai().getIdchuongtrinhkhuyenmai())){
                 if (listkm.get(i).getSoluongmua()<=soluong)
                     return 1;
             }
         }
         return 0;
     }
     
     public static int GetPhanTramKhuyenMai1(String masp){
       List<Chuongtrinhkhuyenmai> listkm = ChiTietKhuyenMaiDAO.layDanhSachChuongTrinhKhuyenMai();
       List<Hinhthuckhuyenmai1> listkmm = HinhThucKhuyenMai1DAO.layDanhSachHinhThucKhuyenMai1();
       for (int i=0;i<listkm.size();i++){
           if (masp.equals(listkm.get(i).getMathang().getIdmathang())){
               String makm = listkm.get(i).getIdchuongtrinhkhuyenmai();
               for (int j=0;j<listkmm.size();j++){
                   if (makm.equals(listkmm.get(j).getChuongtrinhkhuyenmai().getIdchuongtrinhkhuyenmai())){
                       return listkmm.get(j).getGiamgia();
                   }
               }
           }
       }
        
       return 0;
     }
     
     public static void GanSoLuongToiDaToiThieu(String mamh,int toida, int toithieu){
         Session session = HibernateUtil.getSessionFactory().openSession();
         Mathang mh = MatHangDAO.GetMatHang(mamh);
         mh.setSoluongtonkhotoithieu(toithieu);
         mh.setSoluongtonkhotoida(toida);
         try {
            session.beginTransaction();
            session.update(mh);
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
            
        } finally {
            session.close();
        }
     }
     
}
